package com.alan.shiro.api.service;

import com.alan.shiro.api.bean.Permission;
import com.alan.shiro.api.bean.Role;
import com.alan.shiro.api.bean.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: Alan liu
 * @Date: 2018/12/11 19:20
 * @Description: UserService
 */
@Service
public class UserService {

    private static List<User> userLists = new ArrayList<>();

    static {
        //初始化数据
        List<Permission> permissionsAll = new ArrayList<>();
        permissionsAll.add(new Permission(1L,"create",null));
        permissionsAll.add(new Permission(1L,"update",null));
        permissionsAll.add(new Permission(1L,"select",null));
        permissionsAll.add(new Permission(1L,"delete",null));
        Role roleUserAll = new Role();
        roleUserAll.setId(1L);
        roleUserAll.setRoleName("user");
        roleUserAll.setPermissions(permissionsAll);

        Role roleUserView = new Role();
        roleUserView.setId(2L);
        roleUserView.setRoleName("user");
        roleUserView.setPermissions(Arrays.asList(new Permission(1L,"select",null)));

        User user = new User();
        user.setId(1L);
        user.setName("alan");
        user.setPassword("123456");
        user.setRoles(Arrays.asList(roleUserAll));

        User userView = new User();
        userView.setId(2L);
        userView.setName("alanView");
        userView.setPassword("123456");
        userView.setRoles(Arrays.asList(roleUserView));

        User admin = new User();
        admin.setId(3L);
        admin.setName("admin");
        admin.setPassword("123456");
        admin.setRoles(Arrays.asList(roleUserAll));

        userLists.add(user);
        userLists.add(userView);
        userLists.add(admin);
    }


    public User findByName(String name) {
        for (User user : userLists){
            if(user.getName().equals(name)){
                return user;
            }
        }
        return null;
    }

    public User findByUserId(Long userId) {
        for (User user : userLists){
            if(user.getId().equals(userId)){
                return user;
            }
        }
        return null;
    }

    public String getPassword(String username) {
        User user = findByName(username);
        if(user != null){
            return user.getPassword();
        }
        return null;
    }

    /**
     * 根据用户id获取用户信息
     * @param userId
     * @return
     */
    public User select(Long userId) {
        return findByUserId(userId);
    }

    public User select(String principal) {
        return findByName(principal);
    }

    public List<String> selectRoles(String username) {
        List<String> strList = new ArrayList<>();
        List<Role> roles = findByName(username).getRoles();
        if(roles != null){
            for (Role item: roles) {
                strList.add(item.getRoleName());
            }
        }
        return strList;
    }

    public List<String> selectRoles(Long userId) {
        List<String> strList = new ArrayList<>();
        List<Role> roles = findByUserId(userId).getRoles();
        if (roles != null) {
            for (Role item : roles) {
                strList.add(item.getRoleName());
            }
        }
        return strList;
    }



}
